<template>
  <div>
    <!-- 搜索页面头部 -->
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="$router.back()"
      />
      <!-- 搜索组件 -->
      <van-search
        v-model.trim="kw"
        placeholder="请输入搜索关键词"
        background="#007BFF"
        shape="round"
        v-focus
        @input="inputFN"
        @search="searchgoPath"
      />
    </div>
    <!-- 联想列表 -->
    <template v-if="kw">
      <div class="sugg-list" v-for="(item, ide) in SuggestList" :key="ide" @click="Suggest(item)">
      <div class="sugg-item" v-html="lightFn(item, kw)"></div>
    </div>
    </template>
    <!-- 搜索历史 -->
    <div class="search-history" v-else>
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="history=[]"/>
        </template>
      </van-cell>
      <!-- 历史列表 -->
      <div class="history-list">
        <span class="history-item" v-for="(item,index) in history" :key='index' @click="historygoPath(item)">{{item}}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { suggestListAPI } from '@/api/index'
import { constinpputFn, lightFn } from '@/utils/comm' // 防抖回调
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Search',
  data () {
    return {
      kw: '', // 搜索关键字
      SuggestList: [],
      history: JSON.parse(localStorage.getItem('his')) || [] // 历史记录
    }
  },

  mounted () {},

  methods: {
    lightFn,
    // constinpputFn,
    // 输入框实时改变触发方法
    inputFN () {
      constinpputFn(this.kw, this.getSuggestList)
    },
    async getSuggestList () {
      this.SuggestList = []
      if (!this.kw.length) return
      const {
        data: {
          data: { options }
        }
      } = await suggestListAPI({ keyWord: this.kw || '' })
      this.SuggestList = options || []
    },
    // 搜索跳转
    searchgoPath (val) {
      if (!val) return
      this.history.unshift(this.kw)
      this.moveFn(val)
    },
    // 联想字跳转
    Suggest (val) {
      this.history.unshift(val)
      this.moveFn(val)
    },
    // 历史记录跳转
    historygoPath (val) {
      this.moveFn(val)
    },
    // 跳转的时候会清楚实例，所以需要转成定时器的宏任务最后才执行
    moveFn (theKW) {
      setTimeout(() => {
        this.$router.push({
          path: `/Searchpach/${theKW}`
        })
      }, 100)
    }
  },
  watch: {
    history: {
      deep: true,
      handler () {
        // 发现变化立刻覆盖式的存档本地
        // ES6新增的去重方法（set，map）
        // set: 无序的不重复的value集合体，无下角标，返回无法重复的set对象
        // console.log(new Set(this.history)) ====> {'好'}   需要转为数组Array.from（）
        // console.log(Array.from(new Set(this.history)))   ===>['好']
        const arr = Array.from(new Set(this.history))
        localStorage.setItem('his', JSON.stringify(arr))
      }
    }
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
/*搜索建议列表样式 */
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
/**搜索历史 */
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
